package com.ruoyi.service.service.impl;

import com.ruoyi.service.domain.*;
import com.ruoyi.service.entity.item.*;
import com.ruoyi.service.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@Component
public class PcItemService {


    @Autowired
    private ServiceOrdersMapper ordersMapper;

    @Autowired
    private ServiceNurseMapper nurseMapper;

    @Autowired
    private ServiceMycartMapper mycartMapper;

    @Autowired
    private ServiceItemscomboMapper itemscomboMapper;

    @Autowired
    private ServiceComboMapper comboMapper;

    @Autowired
    private ServiceItemsMapper itemsMapper;

    @Autowired
    private ServiceAddressMapper addressMapper;

    @Autowired
    private ServicePayroadMapper payroadMapper;

    @Autowired
    private ServiceOrderscomboMapper orderscomboMapper;

    @Autowired
    private ServiceOrdersitemsMapper ordersitemsMapper;

    @Autowired
    private ServicePackagescomboMapper packagescomboMapper;

    @Autowired
    private ServicePackagesMapper packagesMapper;

    @Autowired
    private ServiceOrderspackagesMapper orderspackagesMapper;

    @Autowired
    private ServicePayMapper payMapper;

    @Autowired
    private ServiceAssessmentMapper assessmentMapper;

    @Autowired
    private ServicePictureMapper pictureMapper;

    @Autowired
    private ServiceCodeMapper codeMapper;

    @Autowired
    private ServiceRefuseMapper refuseMapper;

    @Autowired
    private ServiceOrdernurseMapper ordernurseMapper;

    @Autowired
    private ServiceServedMapper servedMapper;

    @Autowired
    private ServiceUsersMapper usersMapper;

    @Autowired
    private ServiceReasonrefuseMapper reasonrefuseMapper;

    @Autowired
    private ServiceOrderotherpayMapper orderotherpayMapper;

    @Autowired
    private ServiceTimeMapper timeMapper;

    @Autowired
    private ServiceReasonstopMapper reasonstopMapper;

    @Autowired
    private ServiceActionMapper actionMapper;

    @Autowired
    private ServiceLogMapper logMapper;

    @Autowired
    private ServiceReasonMapper reasonMapper;

    @Autowired
    private ServiceCategoryMapper categoryMapper;

    @Autowired
    private ServiceCategoryitemsMapper categoryitemsMapper;

    @Autowired
    private ServicePackagessuppliesMapper packagessuppliesMapper;

    @Autowired
    private ServiceSuppliesMapper suppliesMapper;



    @Transactional //需要
    public List<PCCategoryVO> getItemList(String keywords, String status)
    {
        List<PCCategoryVO> retCateList = new ArrayList<PCCategoryVO>();
        try {
            List<PCCategoryVO> retCategoryList = new ArrayList<>();
            List<PCCategoryVO> newCategoryList = new ArrayList<>();
            List<ServiceCategory> oldCategoryList = categoryMapper.selectServiceCategoryList(null);
            for (ServiceCategory treeNode : oldCategoryList) {
                PCCategoryVO newCategoryNode = new PCCategoryVO();
                newCategoryNode.init(treeNode);

                newCategoryList.add(newCategoryNode);
            }
            for (PCCategoryVO treeNode : newCategoryList) {
                if (null == treeNode.getCategoryLink() || "".equals(treeNode.getCategoryLink())) {
                    retCategoryList.add(findChildren(treeNode, newCategoryList));
                }
            }
            List<PCCategoryVO> newList = new ArrayList<>();
            newList.add(retCategoryList.get(0));
            findItem(newList, keywords, status);

            return newList;

        }catch (Exception e){
            return null;
        }
    }

    static PCCategoryVO findChildren(PCCategoryVO category, List<PCCategoryVO> categoryList) {
        for (PCCategoryVO item : categoryList) {
            if (category.getCategoryId().equals(item.getCategoryLink())) {
                if (category.getChildren() == null) {
                    category.setChildren(new ArrayList<PCCategoryVO>());
                }
                category.getChildren().add(findChildren(item, categoryList));
            }
        }
        return category;
    }

    public void findItem(List<PCCategoryVO> categoryList, String keywords, String status) {
        for (PCCategoryVO category : categoryList) {
            int itemNum = 0;
            if (category.getChildren() != null) {
                findItem(category.getChildren(), keywords, status);
            }
            List<ServiceCategoryitems> categoryitemsList = categoryitemsMapper.selectCategoryitemsByCategoryId(category.getCategoryId());
            for (ServiceCategoryitems categoryitems : categoryitemsList) {
                ServiceItems item = itemsMapper.selectServiceItemsByItemIdNew(categoryitems.getCategoryitemsItemid());
                if (item == null) continue; //空指针
                if (keywords == null) {
                    keywords = "";
                }
                if(status == null){
                    PCItemVO itemNode = new PCItemVO(item);
                    if(category.getItems() == null) {
                        category.setItems(new ArrayList<PCItemVO>());
                    }
                    category.getItems().add(itemNode);
                    itemNum++;
                }else{
                    if ("normal".equals(status)) status = "0";
                    if ("stop".equals(status)) status = "1";


                    if (item.getItemUseFlag().equals(status) && item.getItemName().contains(keywords)) {
                        PCItemVO itemNode = new PCItemVO(item);
                        if(category.getItems() == null) {
                            category.setItems(new ArrayList<PCItemVO>());
                        }
                        category.getItems().add(itemNode);
                        itemNum++;
                    }
                }
            }
            if (itemNum == 0) {
                category.setItems(null);
                category.setItemNum(null);
            }else {
                category.setItemNum(Long.valueOf(itemNum));
            }
        }
    }

    @Transactional
    public PCItemVO getItemDetail(Long itemId) {
        try {
            if (itemId == null){
                itemId = 0L;
            }
            ServiceItems items = itemsMapper.selectServiceItemsByItemIdNew(itemId);
            PCItemVO itemNode = new PCItemVO(items);
            List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectByItemId(itemId);
            for (ServiceItemscombo itemscombo : itemscomboList){
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemscombo.getItemscomboSuppliesid());
                PCComboVO comboNode = new PCComboVO(combo);

                List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
                for (ServicePackagescombo packagescombo : packagescomboList){
                    ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                    PCPackagesVO packagesNode = new PCPackagesVO(packages);
                    //新增两个字段， 空null 改1
                    Long pkgNum = packagescombo.getPackagescomboPackagesnum() == null ? 1 : packagescombo.getPackagescomboPackagesnum();
                    packagesNode.setPkgDetail(packages.getPackagesName() + "*" + pkgNum + ";");
                    packagesNode.setPkgNum(pkgNum);
                    if (comboNode.getPackages() == null) {
                        comboNode.setPackages(new ArrayList<PCPackagesVO>());
                    }
                    comboNode.getPackages().add(packagesNode);
                }

                if (itemNode.getSpecifications() == null){
                    itemNode.setSpecifications(new ArrayList<PCComboVO>());
                }
                itemNode.getSpecifications().add(comboNode);
            }

            return itemNode;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public Integer addCategory(String categoryName, Long linkId) {
        try {
            ServiceCategory category = new ServiceCategory();
            category.setCategoryName(categoryName);
            category.setCategoryLink(linkId);

            ServiceCategory link = categoryMapper.selectServiceCategoryByCategoryId(linkId);
            if (link == null){
                return 0;
            }
            category.setCategoryLevel(link.getCategoryLevel() + 1);
            if (link.getCategoryHeadlink() == null){
                category.setCategoryHeadlink(linkId);
            }else{
                category.setCategoryHeadlink(link.getCategoryHeadlink());
            }
            categoryMapper.insertServiceCategory(category);

            return 1;

        }catch (Exception e){
            return 0;
        }

    }

    @Transactional
    public Integer delCategory(Long categoryId) {
        try {
            ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryId);
            if (category == null){
                return 0;
            }
            category.setCategoryUseFlag("1");
            category.setCategoryDelFlag("1");
            categoryMapper.updateServiceCategory(category);

            return 1;

        }catch (Exception e){
            return 0;
        }
    }

    @Transactional
    public Integer yesCategory(Long categoryId) {
        try {
            ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryId);
            if (category == null || "1".equals(category.getCategoryDelFlag())) {
                return 0;
            }
            category.setCategoryUseFlag("0");
            categoryMapper.updateServiceCategory(category);

            return 1;

        } catch (Exception e) {
            return 0;
        }
    }

    @Transactional
    public Integer noCategory(Long categoryId) {
        try {
            ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryId);
            if (category == null || "1".equals(category.getCategoryDelFlag())) {
                return 0;
            }
            category.setCategoryUseFlag("1");
            categoryMapper.updateServiceCategory(category);

            return 1;

        } catch (Exception e) {
            return 0;
        }
    }

    @Transactional
    public Integer addItem(String itemName, Long categoryId) {
        try {
            ServiceItems item = new ServiceItems();
            item.setItemName(itemName);
            item.setItemCategory(categoryId);
            item.setItemDescription("");
            item.setItemTip("预约本服务，需提供经由正规医疗机构开具的医嘱、处方等文件上传平台。");
            item.setItemPicture("https://img0.baidu.com/it/u=1024258047,2987031221&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=326");
            item.setItemCreateBy(getUsername());
            item.setItemCreateTime(new Date());
            item.setItemUpdateBy(getUsername());
            item.setItemUpdateTime(new Date());
            itemsMapper.insertServiceItems(item);

            ServiceCategoryitems categoryitems = new ServiceCategoryitems();
            categoryitems.setCategoryitemsCategoryid(categoryId);
            categoryitems.setCategoryitemsItemid(item.getItemId());
            categoryitemsMapper.insertServiceCategoryitems(categoryitems);

            return 1;

        }catch (Exception e){
            return 0;
        }

    }

    @Transactional
    public Integer updateItem(String itemName, String desc, String tip, String pic, Long itemId) {
        try {
            ServiceItems item = itemsMapper.selectServiceItemsByItemIdNew(itemId);
            if (item == null){
                return 0;
            }
            if (itemName != null)  item.setItemName(itemName);
            if (desc != null)  item.setItemDescription(desc);
            if (tip != null)  item.setItemTip(tip);
            if (pic != null)  item.setItemPicture(pic);

            item.setItemUpdateBy(getUsername());
            item.setItemUpdateTime(new Date());
            itemsMapper.updateServiceItems(item);

            return 1;

        }catch (Exception e){
            return 0;
        }

    }

    @Transactional
    public Integer delItem(Long itemId) {
        try {
            ServiceItems item = itemsMapper.selectServiceItemsByItemIdNew(itemId);
            if (item == null){
                return 0;
            }
            if ("1".equals(item.getItemDelFlag()))  return 0;

            item.setItemDelFlag("1");

            item.setItemUpdateBy(getUsername());
            item.setItemUpdateTime(new Date());
            itemsMapper.updateServiceItems(item);

            return 1;

        }catch (Exception e){
            return 0;
        }

    }

    @Transactional
    public Integer yesItem(Long itemId) {
        try {
            ServiceItems item = itemsMapper.selectServiceItemsByItemIdNew(itemId);
            if (item == null) {
                return 0;
            }
            if ("1".equals(item.getItemDelFlag())) return 0;
            if ("0".equals(item.getItemUseFlag())) return 2;

            item.setItemUseFlag("0");

            item.setItemUpdateBy(getUsername());
            item.setItemUpdateTime(new Date());
            itemsMapper.updateServiceItems(item);

            return 1;

        } catch (Exception e) {
            return 0;
        }
    }


    @Transactional
    public Integer noItem(Long itemId) {
        try {
            ServiceItems item = itemsMapper.selectServiceItemsByItemIdNew(itemId);
            if (item == null) {
                return 0;
            }
            if ("1".equals(item.getItemDelFlag())) return 0;
            if ("1".equals(item.getItemUseFlag())) return 2;

            item.setItemUseFlag("1");

            item.setItemUpdateBy(getUsername());
            item.setItemUpdateTime(new Date());
            itemsMapper.updateServiceItems(item);

            return 1;

        } catch (Exception e) {
            return 0;
        }
    }

    @Transactional
    public Integer updateCategory(Long categoryId, String categoryName, Long linkId) {
        try {
            ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryId);
            if (categoryName != null) category.setCategoryName(categoryName);
            if (linkId != null){
                category.setCategoryLink(linkId);
                ServiceCategory link = categoryMapper.selectServiceCategoryByCategoryId(linkId);
                if (link == null){
                    return 0;
                }
                category.setCategoryLevel(link.getCategoryLevel() + 1);
                if (link.getCategoryHeadlink() == null){
                    category.setCategoryHeadlink(linkId);
                }else{
                    category.setCategoryHeadlink(link.getCategoryHeadlink());
                }

            }

            categoryMapper.updateServiceCategory(category);

            return 1;

        }catch (Exception e){
            return 0;
        }



    }

    @Transactional
    public List<PCPackagesList> packagesList() {
        try {
            List<PCPackagesList> retList = new ArrayList<>();
            ServicePackages search = new ServicePackages();
            search.setPackagesDelFlag("0");
            search.setPackagesUseFlag("0");
            List<ServicePackages> packagesList = packagesMapper.selectServicePackagesList(search);
            for (ServicePackages packages : packagesList) {
                PCPackagesList retNode = new PCPackagesList(packages);
                List<ServicePackagessupplies> pakSupplies = packagessuppliesMapper.selectServicePackagessuppliesByPackagesId(packages.getPackagesId());
                for (ServicePackagessupplies pakSupply : pakSupplies) {
                    ServiceSupplies supply = suppliesMapper.selectServiceSuppliesBySuppliesId(pakSupply.getPackagessuppliesSuppliesid());
                    retNode.setPkgDetail(retNode.getPkgDetail() + supply.getSuppliesName() + "*" + pakSupply.getPackagessuppliesCount() + ";");
                }
                retList.add(retNode);
            }

            return retList;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public Integer delCombo(Long itemId, Long comboId) {
        try {
            ServiceItemscombo search = new ServiceItemscombo();
            search.setItemscomboItemid(itemId);
            search.setItemscomboSuppliesid(comboId);
            search.setItemscomboDelFlag("0");
            search.setItemscomboUseFlag("0");
            List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(search);
            if (itemscomboList == null || itemscomboList.size() == 0){
                return 0;
            }
            ServiceItemscombo itemscombo = itemscomboList.get(1);
            itemscombo.setItemscomboDelFlag("1");
            itemscombo.setItemscomboUseFlag("1");
            itemscombo.setItemscomboUpdateBy(getUsername());
            itemscombo.setItemscomboUpdateTime(new Date());
            itemscomboMapper.updateServiceItemscombo(itemscombo);
            return 1;

        }catch (Exception e){
            return 0;
        }
    }

    @Transactional
    public PCComboRsp comboDetail(Long comboId) {
        try {
            ServiceCombo combo = comboMapper.selectServiceComboByComboId(comboId);
            PCComboRsp comboNode = new PCComboRsp(combo);

            List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
            for (ServicePackagescombo packagescombo : packagescomboList){
                ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                PCPackagesList packagesNode = new PCPackagesList(packages);
                packagesNode.setPkgDetail(packagesNode.getPkgDetail() + packages.getPackagesName() + "*" + packagescombo.getPackagescomboPackagesnum() + ";");
                packagesNode.setPkgNum(packagescombo.getPackagescomboPackagesnum());
                if (comboNode.getPackages() == null){
                    comboNode.setPackages(new ArrayList<PCPackagesList>());
                }
                comboNode.getPackages().add(packagesNode);
            }

            return comboNode;

        }catch (Exception e){
            return null;
        }


    }


    @Transactional
    public Integer delPackages(Long pckId, Long comboId) {
        try {
            ServicePackagescombo search = new ServicePackagescombo();
            search.setPackagescomboId(comboId);
            search.setPackagescomboPackagesid(pckId);

            List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(search);
            if (packagescomboList == null || packagescomboList.size() == 0){
                return 0;
            }
            ServicePackagescombo packagescombo = packagescomboList.get(1);
            packagescomboMapper.deleteServicePackagescomboByPackagescomboId(packagescombo.getPackagescomboId());

            return 1;

        }catch (Exception e){
            return 0;
        }

    }

    @Transactional
    public Integer saveCombo(PCComboReq reqBody) {
        try {
            if ("new".equals(reqBody.getAddFlag())){
                ServiceCombo combo = new ServiceCombo();
                combo.setComboName(reqBody.getComboName());
                combo.setComboPrice(reqBody.getPrice());
                combo.setComboCurrentprice(reqBody.getNowPrice());
                combo.setComboRate(reqBody.getNowPrice() / reqBody.getPrice());
                combo.setComboIllustrate(reqBody.getDesc());
                combo.setComboCreateBy(getUsername());
                combo.setComboCreateTime(new Date());
                combo.setComboUpdateBy(getUsername());
                combo.setComboUpdateTime(new Date());

                comboMapper.insertServiceCombo(combo);

                ServiceItemscombo itemscombo = new ServiceItemscombo();
                itemscombo.setItemscomboItemid(reqBody.getItemId());
                itemscombo.setItemscomboSuppliesid(reqBody.getComboId());
                itemscombo.setItemscomboCreateBy(getUsername());
                itemscombo.setItemscomboCreateTime(new Date());
                itemscombo.setItemscomboUpdateBy(getUsername());
                itemscombo.setItemscomboUpdateTime(new Date());

                itemscomboMapper.insertServiceItemscombo(itemscombo);//新增服务项目与套餐对应关联

                for (PCPackagesReq packages : reqBody.getPackages()){
                    ServicePackagescombo packagescombo = new ServicePackagescombo();
                    packagescombo.setPackagescomboComboid(combo.getComboId());
                    packagescombo.setPackagescomboPackagesid(packages.getId());
                    if (packages.getId() != null) { //不为空更新，
                        ServicePackages pck = packagesMapper.selectServicePackagesByPackagesId(packages.getId());
                        packagescombo.setPackagescomboPrice(pck.getPackagesPrice() * packages.getNum());
                        packagescombo.setPackagescomboPackagesnum(packages.getNum());
                        packagescomboMapper.insertServicePackagescombo(packagescombo);
                    }
                }


            }else{
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(reqBody.getComboId());
                combo.setComboName(reqBody.getComboName());
                combo.setComboPrice(reqBody.getPrice());
                combo.setComboCurrentprice(reqBody.getNowPrice());
                combo.setComboRate(reqBody.getNowPrice() / reqBody.getPrice());
                combo.setComboIllustrate(reqBody.getDesc());
                combo.setComboUpdateBy(getUsername());
                combo.setComboUpdateTime(new Date());
                comboMapper.updateServiceCombo(combo);

                ServiceItemscombo itemscombo = new ServiceItemscombo();
                itemscombo.setItemscomboItemid(reqBody.getItemId());
                itemscombo.setItemscomboSuppliesid(reqBody.getComboId());
                ServiceItemscombo itemscomboUpdate = itemscomboMapper.selectServiceItemscomboByItemsAndCombo(reqBody.getItemId(), reqBody.getComboId());
                if (itemscomboUpdate != null){
                    itemscombo.setItemscomboId(itemscomboUpdate.getItemscomboId());
                    itemscombo.setItemscomboUpdateBy(getUsername());
                    itemscombo.setItemscomboUpdateTime(new Date());
                    itemscomboMapper.updateServiceItemscombo(itemscombo);
                }

                List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
                for (ServicePackagescombo packagescombo : packagescomboList){
                    packagescomboMapper.deleteServicePackagescomboByPackagescomboId(packagescombo.getPackagescomboId());
                }
                for (PCPackagesReq packages : reqBody.getPackages()){
                    ServicePackagescombo packagescombo = new ServicePackagescombo();
                    packagescombo.setPackagescomboComboid(combo.getComboId());
                    packagescombo.setPackagescomboPackagesid(packages.getId());
                    if (packages.getId() != null) { //不为空更新
                        ServicePackages pck = packagesMapper.selectServicePackagesByPackagesId(packages.getId());
                        packagescombo.setPackagescomboPrice(pck.getPackagesPrice() * packages.getNum());
                        packagescombo.setPackagescomboPackagesnum(packages.getNum());
                        packagescomboMapper.insertServicePackagescombo(packagescombo);
                    }
                }
            }

            return 1;

        } catch (Exception e) {
            return 0;
        }

    }
}
